package com.graduationdesign.zengyan.graduationdesignapp.ui.activities.commonlyUsed;

import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.graduationdesign.zengyan.graduationdesignapp.R;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.dataBase.RecentlyUsed;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.PresenterInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.UiInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseFragment;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.commonAdapter.CommonAdapter;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.commonAdapter.MultiItemTypeAdapter;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.commonAdapter.base.ViewHolder;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.CommonErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.contact.ProfileContact;
import com.graduationdesign.zengyan.graduationdesignapp.ui.presenter.ProfilePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ZengYan on 2018/4/22.
 *
 * @desc :
 */
public class FeedFragment extends BaseFragment implements ProfileContact.ShowRecentlyUsed {

    @BindView(R.id.view_pager)
    ViewPager viewPager;

//
//    @BindView(R.id.base_ui)
//    BaseUiView baseUiView;


    @BindView(R.id.no_result)
    TextView noResult;

    @BindView(R.id.used_list)
    RecyclerView usedList;

    ProfileContact.Presenter presenter;

    private List<String> mData = new ArrayList<String>();

    private int color_array[] = {
            R.color.deep_orange_800,
            R.color.blue_500,
            R.color.amber_800,
            R.color.deep_purple_500
    };
    private List<RecentlyUsed> mUsedList = new ArrayList<>();
    private CommonAdapter<RecentlyUsed> commonAdapter;


    @Override
    protected int provideLayoutId() {
        return R.layout.commonly_used_layout;
    }

    @Override
    protected String provideTag() {
        return "FeedFragment";
    }

    @Override
    protected void initData() {
        presenter = PresenterInjection.provideProfilePresenter(
                ProfilePresenter.newBuilder()
                        .setBaseErrorView(new CommonErrorView(getContext()))
                        .setShowRecentlyUsed(this)
        );
    }

    @Override
    protected void initView(View view) {
        initViewPaper();

        usedList.setLayoutManager(new LinearLayoutManager(getContext()));
        usedList.setNestedScrollingEnabled(false);
        commonAdapter = new CommonAdapter<RecentlyUsed>(getContext(),R.layout.item_people,mUsedList) {
            @Override
            protected void convert(ViewHolder holder, RecentlyUsed recentlyUsed, int position) {
                holder.setText(R.id.name, recentlyUsed.getName());
                holder.setText(R.id.description, recentlyUsed.getDesc());
                holder.setImageUri(R.id.avatar, recentlyUsed.getAvatar());

            }
        };
        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                getActivity().startActivity(UiInjection.provideProfile(getActivity(),mUsedList.get(position).getStuNum()));
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        usedList.setAdapter(commonAdapter);
    }

    private void initViewPaper() {

            mData.add("「最新活动」");
            mData.add("「学校公告」");
            mData.add("「二手市场」");
            mData.add("「校园周边好货」");

        SlideCardAdapter<String> slideCardAdapter = new SlideCardAdapter<String>(getActivity(),R.layout.item_slice_card,mData) {
            @Override
            public void bind(View rootview, String data, int position) {
                 RelativeLayout mLytParent;
                CardView mCard;
                TextView mTitle;
                mLytParent = (RelativeLayout) rootview.findViewById(R.id.lyt_parent);
                mCard = (CardView) rootview.findViewById(R.id.card);
                mTitle = (TextView) rootview.findViewById(R.id.title);

                mTitle.setText(data);
                mCard.setCardBackgroundColor(getResources().getColor(color_array[position]));

            }
        };
        viewPager.setAdapter(slideCardAdapter);
        viewPager.setPageMargin(getResources().getDimensionPixelOffset(R.dimen.viewpager_margin_overlap_payment));
        viewPager.setOffscreenPageLimit(slideCardAdapter.getCount());
    }


    @Override
    public void onVisible() {
        super.onVisible();
        presenter.loadRecentlyUsed();
    }


    @Override
    public void showRecentlyUsed(List<RecentlyUsed> list) {
        mUsedList.clear();
        mUsedList.addAll(list);
        commonAdapter.notifyDataSetChanged();
        if (list.isEmpty()) {
            noResult.setVisibility(View.VISIBLE);
        }else {
            noResult.setVisibility(View.GONE);
        }

    }
}
